const User = require('../models/userModel');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcryptjs');

const register = async (req, res) => {
    try {
        const {username, password, role} = req.body;

        const hashedPassword = await bcrypt.hash(password, 10);

        const newUser = new User({username, password:hashedPassword, role});
        console.log(newUser);
        await newUser.save();
        res.status(201).json({message: `User registered with username ${username}`});
    } catch (err) {
        res.status(500).json({message: `something went wrong: ${err}`});
    }
}

const login = async (req, res) => {
    try {
        const {username, password} = req.body;
        const user = await User.findOne({username});

        if (!user) {
            return res.status(404).json({message: `Invalid credentials`});
        }

        const isMatch = await bcrypt.compare(password, user.password);
        if (!isMatch) {
            return res.status(400).json({message: `User with username ${username} does not exist`});
        }

        // From mongoDB
        const token = jwt.sign(
            {id:user._id,role:user.role},
            process.env.JWT_SECRET,
            {expiresIn: '1h'}
        );

        res.status(200).json({token});

    }catch(err) {
        res.status(500).json({message: `something went wrong: ${err}`});
    }
}

module.exports = {
    register,
    login,
}